import pandas as pd


def getStockCodeList(filename) :
    codeList = []
    with open(filename, 'r') as f :
        for line in f: 
            parts = line.split('\t')
            if len(parts) == 0:
                continue

            code = parts[0]
            if not code.isnumeric() :
                continue

            codeList.append(code)
    
    return codeList


def getStockInfoList(filename, cols = ['代码', '名称', '现价']) :
    colList = {}
    codeList = []
    with open(filename, 'r') as f :
        for line in f: 
            parts = line.split('\t')
            if len(parts) == 0:
                continue

            code = parts[0]
            if not code.isnumeric() :
                if code == '代码':
                    for i, val in enumerate(parts) :
                        if val in cols:
                            colList[val] = i
                continue

            ent = {}
            for col in cols :
                ent[col] = parts[colList[col]]
            codeList.append(ent)
    
    return codeList

def getStockInfoDataFrame(filename, cols = ['代码', '名称', '现价']) :
    data = getStockInfoList(filename, cols)
    dfi = pd.DataFrame(data)
    if '现价' in dfi:
        dfi['现价'] = pd.to_numeric(dfi['现价'], errors='coerce')
    return dfi